<template>
  <div class="adverList-main">
    <!-- 广告 -->
    <el-card shadow="never" class="margin-30" :body-style="{ padding: '20px' }">
      <div slot="header">
        <el-button type="primary" size="small" @click="doAdd">新增</el-button>
      </div>
      <el-table :data="list" border stripe>
        <el-table-column
          label="序号"
          type="index"
          width="50"
          align="center"
        />
        <el-table-column
          label="广告图片"
          width="150"
          align="center"
        >
          <template slot-scope="scope">
            <el-image style="width: 60px; height: 60px;" :src="scope.row.pic" />
          </template>
        </el-table-column>
        <el-table-column
          label="时间"
          align="center"
        >
          <template slot-scope="scope">
            <div>开始时间: {{ scope.row.createTime }}</div>
            <div>结束时间: {{ scope.row.endTime }}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="广告名称"
          prop="name"
          width="150"
          align="center"
        />
        <el-table-column
          label="点击数"
          prop="clickCount"
          width="100"
          align="center"
        />
        <el-table-column
          label="下载数"
          prop="sort"
          width="100"
          align="center"
        />
        <el-table-column
          label="下单数"
          prop="orderCount"
          width="100"
          align="center"
        />
        <el-table-column
          label="轮播位置"
          prop="clickCount"
          width="100"
          align="center"
        />
        <el-table-column
          label="链接地址"
          prop="url"
          width="100"
          align="center"
        />
        <el-table-column
          label="备注"
          prop="note"
          width="100"
          align="center"
        />
        <el-table-column
          label="上下线状态"
          prop="url"
          width="100"
          align="center"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status===1">上线</el-tag>
            <el-tag v-if="scope.row.status===0">下线</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操纵"
          width="100"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button style="color: #f00;" type="text" size="mini" @click="doEditupdateAds(scope.row)">编辑</el-button>
            <el-button type="text" size="mini" @click="doDelAds(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

    </el-card>

    <el-dialog
      title="广告详情"
      :visible.sync="dialogVisible"
      width="60%"
    >
      <el-form ref="form" :model="advertiseList" label-width="100px" size="mini">
        <el-row :gutter="20">
          <el-col :span="10" :offset="0">
            <el-form-item label="广告名称">
              <el-input v-model="advertiseList.name" />
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="0">
            <el-form-item label="备注">
              <el-input v-model="advertiseList.note" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="10" :offset="0">
            <el-form-item label="排序">
              <el-input v-model="advertiseList.sort" />
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="0">
            <el-form-item label="产品数量">
              <el-input v-model="advertiseList.clickCount" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="10" :offset="0">
            <el-form-item label="链接">
              <el-input v-model="advertiseList.url" />
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="0">
            <el-form-item label="产品轮播位置">
              <el-select v-model="advertiseList.type">
                <el-option
                  label="app首页轮播 "
                  :value="1"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="10" :offset="0">
            <el-form-item label="开始时间">
              <div class="block">
                <el-date-picker
                  v-model="advertiseList.startTime "
                  type="date"
                  placeholder="选择日期"
                />
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="0">
            <el-form-item label="结束时间">
              <div class="block">
                <el-date-picker
                  v-model="advertiseList.endTime "
                  type="date"
                  placeholder="选择日期"
                />
              </div>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="10" :offset="0">
            <el-form-item label="上下线状态">
              <!-- status (integer, optional): 上下线状态：0->下线；1->上线 , -->
              <el-switch v-model="advertiseList.status" :active-value="1" :inactive-value="0" />
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="0">
            <el-form-item label="图片">
              <el-upload
                class="avatar-uploader"
                :action="uploadFileOss"
                :header="token"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon" />
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button v-if="isUpdate===false" type="primary" @click="doAddadsList">确 定</el-button>
        <el-button v-if="isUpdate" type="primary" @click="doUpdateAds">点击更新</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import mix from '@/mixins'
import { adsList, addAds, updateAds, delAds } from '@/api/rceommend/adsList'

export default {
  name: 'AdverList',
  mixins: [mix],
  data() {
    return {
      list: [],
      dialogVisible: false,
      imageUrl: '',
      advertiseList: {},
      isUpdate: false
    }
  },

  created() {
    this.getadsList()
  },

  mounted() {

  },

  methods: {
    // 删除广告
    doDelAds(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delAds(id)
        const { success, message } = res
        if (success) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        } else {
          this.$message.error(message)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // 编辑广告
    doEditupdateAds(item) {
      this.isUpdate = true
      this.advertiseList = item
      this.imageUrl = item.pic
      this.dialogVisible = true
    },

    // 编辑广告
    async doUpdateAds() {
      const res = await updateAds(this.advertiseList)
      const { success, message } = res
      if (success) {
        this.$message.success('更新成功')
        this.getadsList()
        this.dialogVisible = false
      } else {
        this.$message.error(message)
      }
    },

    // 新增广告
    doAdd() {
      this.dialogVisible = true
    },
    async doAddadsList() {
      const res = await addAds(this.advertiseList)
      const { success, message } = res
      if (success) {
        this.$message.success('新增成功')
        this.getadsList()
        this.dialogVisible = false
      } else {
        this.$message.error(message)
      }
    },

    // 获取广告列表
    async getadsList() {
      const res = await adsList()
      console.log(res)
      const { data: { items }, success, message } = res
      if (success) {
        this.list = items
      } else {
        this.$message.error(message)
      }
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.advertiseList.pic = URL.createObjectURL(file.raw)
    }
  }
}
</script>

<style lang="scss" scoped>
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
